<?php

_include(_OTEKOO_API_."APIDefault");
_include(_OTEKOO_UTIL_."DBUtil");

class APITalk extends APIDefault
{
	public function __construct($api){
		parent::__construct($api);
	}
	
	/** 
	* Default method
	* @http_method get
	*/
	public function _default()
	{
		$this->create();
	}
	
	/** 
	* create new Talk
	* @http_method post get
	*/
	public function create()
	{
		$code = 201;
		try{
			$email = $this->email($errors);		
			$full_name = $this->name($errors);
			$message = $this->message($errors);
						
			if(empty($errors))
			{
				$this->_db->query(sprintf($this->_scripts->talk, $full_name, $email, $message), $x);
				if(!empty($x))
					$errors[] = $x;
				else
				{
					$result = $this->_db->queryUniqueObject($this->_scripts->talk_key);
					$this->_response->key = $result->ctl_key;
				}
			}
		}catch(Exception $e)
		{
			$errors[] = $e->getMessage();
		}
		if(!empty($errors))
		{
			$code = 400;
			$this->_response->errors = $errors;
		}
		$this->response($code);
	}
	
	/** 
	* send new reply to a talk
	* @http_method post get
	*/
	public function reply()
	{
		$code = 201;
		try{
			$key = $this->key($errors);
			$message = $this->message($errors);
			
			if(empty($errors))
			{
				$this->init_db();
				$this->_db->query(sprintf($this->_scripts->reply, $key, $message), $x);
				if(!empty($x))
					$errors[] = $x;
			}
		}catch(Exception $e)
		{
			$errors[] = $e->getMessage();
		}
		if(!empty($errors))
		{
			$code = 400;
			$this->_response->errors = $errors;
		}
		$this->response($code);
	}
	
	/** 
	* add a folower to a Talk
	* @http_method post get
	*/
	public function follower()
	{
		$code = 202;
		$errors = array();
		try{
			$email = $this->email($errors);		
			$full_name = $this->name($errors);
			$key = $this->key($errors);
			
			if(empty($errors))
			{
				$this->init_db();
				$this->_db->query(sprintf($this->_scripts->follower, $key, $full_name, $email), $x);
				if(!empty($x))
					$errors[] = $x;
				else
				{
					$result = $this->_db->queryUniqueObject($this->_scripts->follower_key);
					$x = $this->_db->error();
					if(!empty($x))
						$errors[] = $x;
					else
						$this->_response->key = $result->ctl_key;
				}
			}
		}catch(Exception $e)
		{
			$errors[] = "ee".$e->getMessage();
		}
		if(!empty($errors))
		{
			$code = 400;
			$this->_response->errors = $errors;
		}
		$this->response($code);
	}
	
	/** 
	* retrieve the roster of a Talk
	* @http_method post get
	*/
	public function followers()
	{
		$code = 200;
		$errors = array();
		try{
			$key = $this->key($errors);
			
			if(empty($errors))
			{
				$this->init_db();
				$this->_db->query(sprintf($this->_scripts->followers, $key), $x);
				if(!empty($x))
					$errors[] = $x;
				else
				{
					$followers = array();
					while($r = $this->_db->fetchNextObject())
					{
						$followers[] = $r;
					}
					$this->_response->followers = $followers;
				}
			}
		}catch(Exception $e)
		{
			$errors[] = $e->getMessage();
		}
		if(!empty($errors))
		{
			$code = 400;
			$this->_response->errors = $errors;
		}
		$this->response($code);
	}
	
	/** 
	* retrieve the message of a Talk
	* @http_method post get
	*/
	public function view()
	{
		$code = 200;
		try{
			$key = $this->key($errors);	
			$page = (int)$this->_request->page;
			if(!isset($page))
				$page = 1;
			
			if(empty($errors))
			{
				$this->init_db();
				$this->_db->query(sprintf($this->_scripts->talks, $key, $page, 10), $x);
				if(!empty($x))
					$errors[] = $x;
				else
				{
					$talks = array();
					while($r = $this->_db->fetchNextObject())
					{
						$talks[] = $r;
					}
					$this->_response->talks = $talks;
				}
			}
		}catch(Exception $e)
		{
			$errors[] = $e->getMessage();
		}
		if(!empty($errors))
		{
			$code = 400;
			$this->_response->errors = $errors;
		}
		$this->response($code);
	}
}